package cn.innosmart.aq.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.tutk.p2p.ConnectionEntity;

import cn.innosmart.aq.R;
import cn.innosmart.aq.adapter.HistoryDeviceListAdapter;
import cn.innosmart.aq.bean.AquariumBean;
import cn.innosmart.aq.bean.DeviceBean;
import cn.innosmart.aq.util.SystemConstant;
import cn.innosmart.aq.view.activity.DeviceHistoryDataChartsActivity;

/**
 * Created by David on 2015/7/23.
 */
public class BoxDetailHistoryFragment extends BaseFragment {
    private View view;
    private ListView lv_device_item;
    private Menu menus;
    private HistoryDeviceListAdapter historyDeviceListAdapter;
    private ConnectionEntity connectionEntity;

    public void setConnectionEntity(ConnectionEntity connectionEntity) {
        this.connectionEntity = connectionEntity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_history, container, false);
        lv_device_item = (ListView) view.findViewById(R.id.lv_device_item);
        historyDeviceListAdapter = new HistoryDeviceListAdapter(getActivity());
        historyDeviceListAdapter.setOnclickListener(mDeletOnClickListener);
        lv_device_item.setOnItemClickListener(onItemClickListener);
        lv_device_item.setAdapter(historyDeviceListAdapter);
        setHasOptionsMenu(true);
        setBar();
        return view;
    }


    /**
     * 设置Toolbar
     */
    private Toolbar toolbar;

    private void setBar() {
        toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
    }

    private boolean isModify = false;
    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            return true;
        }

    };

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        getActivity().getMenuInflater().inflate(R.menu.menu_history, menu);
        menus = menu;
        super.onCreateOptionsMenu(menu, inflater);
    }

    private AquariumBean aquariumBean;

    public void setAquarium(AquariumBean aquarium) {
        this.aquariumBean = aquarium;
    }

    @Override
    public void onResume() {
        getActivity().invalidateOptionsMenu();
        if (aquariumBean != null) {
            historyDeviceListAdapter.refreshData(aquariumBean);
        }
        super.onResume();
    }

    private View.OnClickListener mDeletOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    };

    private final int DEVICES = 2;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DEVICES:
                    DeviceBean deviceBean = (DeviceBean) msg.obj;
                    switchChartActivity(deviceBean);
                    break;
            }
        }


    };
    private void switchChartActivity(DeviceBean deviceBean) {
        Intent intent = new Intent(getActivity(), DeviceHistoryDataChartsActivity.class);
        intent.putExtra("device", deviceBean);
        intent.putExtra("uid", connectionEntity.getUid());
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }
    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String address = aquariumBean.getDeivices().get(position);
            DeviceBean deviceBean = SystemConstant.deviceBeanHashMap.get(address);
            Message msg = mHandler.obtainMessage();
            msg.what = DEVICES;
            msg.obj = deviceBean;
            mHandler.sendMessage(msg);
        }
    };
}
